package com.quark.framework.utility;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;

public class QuarkUtilStringConverter {
	public static enum QUARK_STRCONVERT_TYPE
	{
		CONVERT_SQL,// for sql string
		CONVERT_JAVASCRIPT,// for javascript / json string
		CONVERT_NORMAL,// normal convert
	}
	public static String fromArray(Object[] array, String marker)
	{
		String ret = "";
		for(int i = 0; i < array.length; i++)
		{
			ret += marker + array[i] + marker;
			if(i < array.length - 1)
				ret += ",";
		}
		return ret;
	}
	public static String fromArray(Object[] array)
	{
		try
		{
			JSONArray parseJSON = JSONArray.fromObject(array);
			return parseJSON.toString();
		}
		catch(JSONException e)
		{
			QuarkLog.log(e);
			return null;
		}
	}
	public static String fromList(List<Object> list)
	{
		try
		{
			JSONArray parseJSON = JSONArray.fromObject(list);
			return parseJSON.toString();
		}
		catch(JSONException e)
		{
			QuarkLog.log(e);
			return null;
		}
	}
		
	public static String fromList(List<Object> list, String marker)
	{
		String ret = "";
		for(int i = 0; i < list.size(); i++)
		{
			ret += marker + list.get(i) + marker;
			if(i < list.size() - 1)
				ret += ",";
		}
		return ret;		
	}
		
	@SuppressWarnings("unchecked")
	public static List<Object> getListFromDBArray(String strDbArray)
	{
		JSONArray array = JSONArray.fromObject(strDbArray);
		return new ArrayList<Object>(JSONArray.toCollection(array));
	}
	
	@SuppressWarnings("unchecked")
	public static Object[] getArrayFromDBArray(String strDbArray)
	{
		JSONArray array = JSONArray.fromObject(strDbArray);
		return (Object[]) JSONArray.toArray(array);
	}
	
	@SuppressWarnings("unchecked")
	public static String convertString(Object value, QUARK_STRCONVERT_TYPE convertType)
	{
		if(value == null)
		{
			return null;
		}
		else if(value.getClass().equals(Date.class))
		{
			switch(convertType)
			{
			case CONVERT_NORMAL:
				return QuarkUtilDateHelper.getDateTimeString((Date)value);
			default:
				return "'" + QuarkUtilDateHelper.getDateTimeString((Date)value) + "'";
			}			
		}
		else if (value.getClass().equals(String.class))
		{
			switch(convertType)
			{
			case CONVERT_NORMAL:
				return value.toString();
			case CONVERT_SQL:
				return "'" + QuarkUtilStringTools.escapeSql(value.toString())+ "'";
			default:
				return "'" + value.toString() + "'";
			}
		}
		else if (value.getClass().equals(Integer.class))
		{
			return ((Integer)value).toString();
		}
		else if (value.getClass().equals(Double.class))
		{
			return ((Double)value).toString();
		}
		else if (value.getClass().equals(Float.class))
		{
			return ((Float)value).toString();
		}
		else if (value.getClass().equals(Boolean.class))
		{
			return ((Boolean)value).toString();
		}		
		else if(value.getClass().equals(List.class))
		{
			switch(convertType)
			{
			case CONVERT_NORMAL:
				return fromList((List<Object>)value, "");
			case CONVERT_JAVASCRIPT:
				return fromList((List<Object>)value, "'");
			case CONVERT_SQL:
				//return "'" + fromList((List<Object>)value, "") + "'";
				return "'" + fromList((List<Object>)value) + "'";
			default:
				QuarkLog.logError("using undefined convert type when converting List 2 String");
				return null;
			}			
		}
		else if(value.getClass().isArray())
		{
			switch(convertType)
			{
			case CONVERT_NORMAL:
				return fromArray((Object[])value, "");
			case CONVERT_JAVASCRIPT:
				return fromArray((Object[])value, "'");
			case CONVERT_SQL:
				return "'" + fromArray((Object[])value, "") + "'";
			default:
				QuarkLog.logError("using undefined convert type when converting Array 2 String");
				return null;
			}		
		}
		else
		{
			QuarkLog.logDebug("try convert " + value.getClass().getCanonicalName() + " to string!");
			return value.toString();
		}
	}
}
